package GUI.UI
{
    import GUI.UI.Events.UIScrollEvent;
    
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class UIScroll extends UIEntity
    {   
        protected var _vertical:Boolean;
        protected var _moreScrollButton:UIButton;
        protected var _lessScrollButton:UIButton;
        protected var _dragScrollButton:UIButton;
        
        public function UIScroll(baseSprite:Sprite, directionVertical:Boolean=true)
        {
            super(baseSprite);
            
            vertical = directionVertical;
        }
        
        public function get vertical():Boolean
        {
            return _vertical;
        }

        public function set vertical(value:Boolean):void
        {
            _vertical = value;
        }
        
        public override function destroy():void
        {
            moreScrollButton = null;
            lessScrollButton = null;
            dragScrollButton = null;
            
            super.destroy();
        }

        public function moreScroll():void
        {
            this.dispatchEvent(new UIScrollEvent(UIScrollEvent.SCROLL_MORE, vertical));
        }

        public function lessScroll():void
        {
            this.dispatchEvent(new UIScrollEvent(UIScrollEvent.SCROLL_LESS, vertical));
        }
        
        public function get moreScrollButton():UIButton
        {
            return _moreScrollButton;
        }

        public function set moreScrollButton(value:UIButton):void
        {
            if(value)
                value.addEventListener(MouseEvent.MOUSE_UP, onButtonClicked);
            else
                value.removeEventListener(MouseEvent.MOUSE_UP, onButtonClicked);

            if(_moreScrollButton)
                _moreScrollButton.removeEventListener(MouseEvent.MOUSE_UP, onButtonClicked);
            
            _moreScrollButton = value;
        }

        public function get lessScrollButton():UIButton
        {
            return _lessScrollButton;
        }

        public function set lessScrollButton(value:UIButton):void
        {
            if(value)
                value.addEventListener(MouseEvent.MOUSE_UP, onButtonClicked);
            else
                value.removeEventListener(MouseEvent.MOUSE_UP, onButtonClicked);
            
            if(_lessScrollButton)
                _lessScrollButton.removeEventListener(MouseEvent.MOUSE_UP, onButtonClicked);
            
            _lessScrollButton = value;
        }

        public function get dragScrollButton():UIButton
        {
            return _dragScrollButton;
        }

        public function set dragScrollButton(value:UIButton):void
        {
            _dragScrollButton = value;
        }

        protected function onButtonClicked(e:MouseEvent):void
        {
            switch (e.target)
            {
                case moreScrollButton.baseSprite:
                    moreScroll();
                break;

                case lessScrollButton.baseSprite:
                    lessScroll();
                break;
                
            }
        }

    }
}